package top.lyc829.zzl_android.ui.message;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.ViewGroup;

import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.grouplist.XUICommonListItemView;
import com.xuexiang.xui.widget.grouplist.XUIGroupListView;

import java.io.IOException;

import okhttp3.Response;
import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.databinding.ActivityMessageBinding;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.service.MsgNumberService;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.ui.main.MainActivity;
import top.lyc829.zzl_android.ui.main.MainViewModel;
import top.lyc829.zzl_android.ui.message.applay.NoticeApplyActivity;
import top.lyc829.zzl_android.ui.message.follow.NoticeFollowActivity;
import top.lyc829.zzl_android.ui.message.praise.NoticePraiseActivity;
import top.lyc829.zzl_android.ui.message.replay.ReplayActivity;
import top.lyc829.zzl_android.ui.message.system.NoticeSystemActivity;

public class MessageActivity extends AppCompatActivity {

    ActivityMessageBinding binding;
    XUIGroupListView mMsgGroupList;
    XUICommonListItemView itemSystem, itemComments, itemPraise, itemFollow, itemApply;

    private MessageViewModel mViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMessageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        mViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(MessageViewModel.class);

        binding.mTitleBarMessage.addAction(new TitleBar.TextAction("全部已读") {
            @Override
            public void performAction(View view) {
                mViewModel.clearMsg();
            }
        });

        mMsgGroupList = findViewById(R.id.msg_group_list);



        initGroupListView();
        observe();
        clickListener();


    }

    private void observe() {
        mViewModel.getMsgNumberLiveData().observe(this, msgNumberBean -> {
            if (msgNumberBean.getSystemMsgNum() == null || msgNumberBean.getSystemMsgNum() <= 0) {
                itemSystem.setDetailText("");
            } else {
                itemSystem.setDetailText(msgNumberBean.getSystemMsgNum().toString());
            }
            if (msgNumberBean.getCommentMsgNum() == null || msgNumberBean.getCommentMsgNum() <= 0) {
                itemComments.setDetailText("");
            } else {
                itemComments.setDetailText(msgNumberBean.getCommentMsgNum().toString());
            }
            if (msgNumberBean.getPraiseMsgNum() == null || msgNumberBean.getPraiseMsgNum() <= 0) {
                itemPraise.setDetailText("");
            } else {
                itemPraise.setDetailText(msgNumberBean.getPraiseMsgNum().toString());
            }
            if (msgNumberBean.getFollowMsgNum() == null || msgNumberBean.getFollowMsgNum() <= 0) {
                itemFollow.setDetailText("");
            } else {
                itemFollow.setDetailText(msgNumberBean.getFollowMsgNum().toString());
            }
            if (msgNumberBean.getApplyMsgNum() == null || msgNumberBean.getApplyMsgNum() <= 0) {
                itemApply.setDetailText("");
            } else {
                itemApply.setDetailText(msgNumberBean.getApplyMsgNum().toString());
            }

        });

    }

    private void initGroupListView() {

        itemSystem = mMsgGroupList.createItemView(
                ContextCompat.getDrawable(this, R.drawable.ic_baseline_system_msg),
                "系统通知",
                null,
                XUICommonListItemView.HORIZONTAL,
                XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        itemComments = mMsgGroupList.createItemView(
                ContextCompat.getDrawable(this, R.drawable.ic_baseline_comments_msg),
                "收到的评论",
                null,
                XUICommonListItemView.HORIZONTAL,
                XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        itemPraise = mMsgGroupList.createItemView(
                ContextCompat.getDrawable(this, R.drawable.ic_baseline_praise_msg),
                "收到的点赞",
                null,
                XUICommonListItemView.HORIZONTAL,
                XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        itemFollow = mMsgGroupList.createItemView(
                ContextCompat.getDrawable(this, R.drawable.ic_baseline_follow_msg),
                "收到的关注",
                null,
                XUICommonListItemView.HORIZONTAL,
                XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);

        itemApply = mMsgGroupList.createItemView(
                ContextCompat.getDrawable(this, R.drawable.ic_baseline_apply_msg),
                "收到的申请",
                null,
                XUICommonListItemView.HORIZONTAL,
                XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);




        View.OnClickListener onClickListener = v -> {
            if (v instanceof XUICommonListItemView) {
                String text = (String) ((XUICommonListItemView) v).getText();
                switch (text) {
                    case "通知设置":
                        break;
                    case "帮助与反馈":
                        break;
                    case "用户协议":
                        break;
                    case "关于":
                        break;
                    case "退出登录":
                        break;
                    default:
                        break;
                }

            }
        };

        int size = DensityUtils.dp2px(this, 20);
        XUIGroupListView.newSection(this)
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addItemView(itemSystem, onClickListener)
                .addItemView(itemComments, onClickListener)
                .addItemView(itemPraise, onClickListener)
                .addItemView(itemFollow, onClickListener)
                .addItemView(itemApply, onClickListener)
                .addTo(mMsgGroupList);



    }

    private void clickListener() {
        binding.mTitleBarMessage.setLeftClickListener(v -> finish());

        itemComments.setOnClickListener(v -> {
            if (!Utils.isLogin()) {
                Utils.showToast("请先登录");
                return;
            }

            Intent intent = new Intent(this, ReplayActivity.class);
            startActivity(intent);
        });

        itemPraise.setOnClickListener(v -> {
            if (!Utils.isLogin()) {
                Utils.showToast("请先登录");
                return;
            }
            Intent intent = new Intent(this, NoticePraiseActivity.class);
            startActivity(intent);
        });

        itemFollow.setOnClickListener(v -> {
            if (!Utils.isLogin()) {
                Utils.showToast("请先登录");
                return;
            }
            Intent intent = new Intent(this, NoticeFollowActivity.class);
            startActivity(intent);
        });

        itemApply.setOnClickListener(v -> {
            if (!Utils.isLogin()) {
                Utils.showToast("请先登录");
                return;
            }
            Intent intent = new Intent(this, NoticeApplyActivity.class);
            startActivity(intent);
        });

        itemSystem.setOnClickListener(v -> {
            Intent intent = new Intent(this, NoticeSystemActivity.class);
            startActivity(intent);
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        mViewModel.updateMsgNumber();
    }
}